package com.hzsd.zengbanyi.biaodan_text;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.hzsd.zengbanyi.biaodan_text.bean.AmmoniaData;
import com.hzsd.zengbanyi.biaodan_text.bean.Body;

import butterknife.Bind;
import butterknife.ButterKnife;
import hzst.android.BaseActivity;
import hzst.android.adapter.CommonAdapter;
import hzst.android.util.DensityUtil;
import hzst.android.util.JsonDataParser;
import hzst.android.util.NetworkDataUtil;
import hzst.android.view.MyListview;
import hzst.android.view.TopNavigationBar;

public class MainActivity extends BaseActivity {
    @Bind(R.id.tv_fxrq)
    TextView tvFxrq;
    @Bind(R.id.tv_sw)
    TextView tvSw;
    @Bind(R.id.tv_sd)
    TextView tvSd;
    @Bind(R.id.ll_BasicParameter)
    LinearLayout llBasicParameter;
    @Bind(R.id.tv_fxffjly)
    TextView tvFxffjly;
    @Bind(R.id.tv_yqmcjbh)
    TextView tvYqmcjbh;
    TextView tv_fxxm, tv_ypxz;
    TextView tv_xmmc;
    TopNavigationBar tnb_detail;
    Spinner sp_fxffjly, sp_yqmcjbh;
    AmmoniaData ammoniaData;
    @Bind(R.id.ls_sjjl)
    MyListview lsSjjl;
    EditText head_fxrq, head_wd, head_sd, body_Volume, body_Dilution, body_A, body_A_Ao;
    CommonAdapter<Body> bodyAdapter;
    Dialog head_dialog;
    Dialog body_dialog;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    tv_xmmc.setText(ammoniaData.getName());
                    tv_fxxm.setText(ammoniaData.getProperties());
                    tv_ypxz.setText(ammoniaData.getAnalysisProject());
                    tnb_detail.setTitle(ammoniaData.getName());
                    tvFxffjly.setText(ammoniaData.getAnalysisMethod());
                    tvYqmcjbh.setText(ammoniaData.getInstrument());
                    setHeadData();
//                    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, ammoniaData.getAnalysisMethod());
//                    sp_fxffjly.setAdapter(adapter1);
//                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, ammoniaData.getInstrument());
//                    sp_yqmcjbh.setAdapter(adapter);
                    bodyAdapter = new CommonAdapter<Body>(MainActivity.this, ammoniaData.getBody(), R.layout.ls_sjjl_layout) {
                        LinearLayout sjjl_xiabufen;
                        Button bianji;
                        String Volume, Dilution, A;

                        @Override
                        public void convert(ViewHolder helper, final Body item, int position) {
                            super.convert(helper, item, position);
                            if (position == 0) {
                                Volume = item.getVolume();
                                Dilution = item.getDilution();
                                A = item.getA();
                            }

                            helper.setText(R.id.sjjl_Code, "样品编号：" + item.getCode());
                            helper.setText(R.id.sjjl_Volume, item.getVolume());
                            helper.setText(R.id.sjjl_Dilution, item.getDilution());
                            helper.setText(R.id.sjjl_A, item.getA());
                            helper.setText(R.id.sjjl_concentration, item.getConcentration());
                            helper.setText(R.id.sjjl_A0, item.getA0());
                            helper.setText(R.id.sjjl_A_Ao, item.getA_Ao());
                            helper.setText(R.id.sjjl_Curve, item.getCurve());
                            helper.setText(R.id.sjjl_AverageConcentration, item.getAverageConcentration());

                            sjjl_xiabufen = helper.getView(R.id.sjjl_xiabufen);
                            bianji = helper.getView(R.id.sjjl_bianji);

                            if (item.isXianshi()) {
                                sjjl_xiabufen.setVisibility(View.VISIBLE);
                            } else {
                                sjjl_xiabufen.setVisibility(View.GONE);
                            }

                            bianji.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {

                                    body_dialog = new Dialog(MainActivity.this);
                                    View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.body_dialog_layout, null);
                                    body_dialog = new Dialog(MainActivity.this, R.style.dialog_transparency);
                                    body_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                                    body_dialog.setContentView(view);
                                    body_dialog.show();
                                    WindowManager.LayoutParams params = body_dialog.getWindow().getAttributes();
                                    params.width = DensityUtil.dip2px(MainActivity.this, 700);
                                    params.height = DensityUtil.dip2px(MainActivity.this, 400);
                                    body_dialog.getWindow().setAttributes(params);

                                    body_Volume = (EditText) view.findViewById(R.id.body_Volume);
                                    body_Dilution = (EditText) view.findViewById(R.id.body_Dilution);
                                    body_A = (EditText) view.findViewById(R.id.body_A);
                                    body_A_Ao = (EditText) view.findViewById(R.id.body_A_Ao);
                                    if (!item.getVolume().equals("")) {
                                        body_Volume.setText(item.getVolume());
                                    } else {
                                        body_Volume.setText(Volume);
                                    }
                                    if (!item.getVolume().equals("")) {
                                        body_Dilution.setText(item.getDilution());
                                    } else {
                                        body_Dilution.setText(Dilution);
                                    }
                                    if (!item.getVolume().equals("")) {
                                        body_A.setText(item.getA());
                                    } else {
                                        body_A.setText(A);
                                    }
                                    body_A_Ao.setText(item.getA_Ao());
                                    Button quxiao = (Button) view.findViewById(R.id.head_quxiao);
                                    quxiao.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            body_dialog.dismiss();
                                        }
                                    });
                                    Button queren = (Button) view.findViewById(R.id.head_queren);
                                    queren.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {

                                            body_dialog.dismiss();
                                        }
                                    });
                                }
                            });
                        }

                    };
                    lsSjjl.setAdapter(bodyAdapter);
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                String shuju = NetworkDataUtil.getJsonData("http://192.168.2.52:6002/ServiceInterface/OriginalRecord/Test.ashx");
                ammoniaData = JsonDataParser.getBean(shuju, AmmoniaData.class);
                Log.i("zby", ammoniaData.toString());
                handler.sendEmptyMessage(1);
            }
        }).start();
        initView();
        initBaseData();
        initEvents();


    }

    @Override
    protected void initBaseData() {

    }

    void setHeadData() {
        tvFxrq.setText(ammoniaData.getAnalysisDate());
        tvSw.setText(ammoniaData.getIndoorT());
        tvSd.setText(ammoniaData.getHumidity());
    }

    @Override
    protected void initView() {
        tv_xmmc = (TextView) findViewById(R.id.tv_xmmc);
        tv_fxxm = (TextView) findViewById(R.id.tv_fxxm);
        tv_ypxz = (TextView) findViewById(R.id.tv_ypxz);
        tnb_detail = (TopNavigationBar) findViewById(R.id.tnb_detail);
//        sp_fxffjly = (Spinner) findViewById(R.id.sp_fxffjly);
//        sp_yqmcjbh = (Spinner) findViewById(R.id.sp_yqmcjbh);
        llBasicParameter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout
                        .head_dialog_layout, null);
                head_dialog = new Dialog(MainActivity.this, R.style.dialog_transparency);
                head_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                head_dialog.setContentView(view);
                head_dialog.show();
                WindowManager.LayoutParams params = head_dialog.getWindow().getAttributes();
                params.width = DensityUtil.dip2px(MainActivity.this, 550);
                params.height = DensityUtil.dip2px(MainActivity.this, 300);
                head_dialog.getWindow().setAttributes(params);
                head_fxrq = (EditText) view.findViewById(R.id.head_fxrq);
                head_sd = (EditText) view.findViewById(R.id.head_sd);
                head_wd = (EditText) view.findViewById(R.id.head_wd);
                head_fxrq.setText(ammoniaData.getAnalysisDate());
                head_sd.setText(ammoniaData.getHumidity());
                head_wd.setText(ammoniaData.getIndoorT());
                Button quxiao = (Button) view.findViewById(R.id.head_quxiao);
                quxiao.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        head_dialog.dismiss();
                    }
                });
                Button queren = (Button) view.findViewById(R.id.head_queren);
                queren.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!ammoniaData.getAnalysisDate().equals(head_fxrq.getText().toString()))
                            ammoniaData.setAnalysisDate(head_fxrq.getText().toString());
                        if (!ammoniaData.getHumidity().equals(head_sd.getText().toString()))
                            ammoniaData.setHumidity(head_sd.getText().toString());
                        if (!ammoniaData.getIndoorT().equals(head_wd.getText().toString()))
                            ammoniaData.setIndoorT(head_wd.getText().toString());
                        setHeadData();
                        head_dialog.dismiss();
                    }
                });
            }
        });

        lsSjjl.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (ammoniaData.getBody().get(position).isXianshi()) {
                    ammoniaData.getBody().get(position).setXianshi(false);
                } else {
                    ammoniaData.getBody().get(position).setXianshi(true);
                }
                bodyAdapter.notifyDataSetChanged();
            }

        });
    }

    @Override
    protected void initEvents() {

    }


}
